﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.IO;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HsTec.Mobile.Green.SqlServerDAL;
using Baimasoft.Web.UI;

public partial class News_NewsFrom : Baimasoft.Web.UI.PageFormTop
{
    NewsBigClass MyNBC = new NewsBigClass();
    NewsSmallClass MyNSC = new NewsSmallClass();
    News MyNews = new News();

    public string sPic = "";


    #region 重写方法


    protected override void BindDDLData()
    {
        ddlBigClassid.DataSource = MyNBC.GetNewsBigClassInfos("");
        ddlBigClassid.DataBind();
        ddlBigClassid.Items.Insert(0, "全部类别");
        ddlBigClassid.Items[0].Value = "0";
    }

    protected void ddlBigClass_SelectedIndexChanged(object sender, EventArgs e)
    {
        string BigId = this.ddlBigClassid.SelectedValue;
        string SqlStr = "";
        if (BigId != "" && BigId != "0")
        {
            SqlStr = "Bigclassid ='" + BigId + "'";
        }
        NewsSmallClass newsSmallClass = new NewsSmallClass();
        DataTable dt = newsSmallClass.GetNewsSmallClassInfos(SqlStr);

    }

    /// <summary>
    /// 初始化表单信息
    /// </summary>
    protected override void InitForm()
    {
        if (!Page.IsPostBack)
        {
            try
            {
                try
                {
                    Indicator.Text = Request.QueryString["sIndicator"].ToString();
                }
                catch
                {
                }

                string sEditStatus = this.Request.QueryString["EditStatus"].ToString().Trim();
                if (sEditStatus == "2")
                {
                    string sID = this.Request.QueryString["ID"].ToString().Trim();
                    DataTable MyDT = MyNews.GetNewsInfos(" a.ID = '" + sID + "' ");
                    foreach (DataRow dr in MyDT.Rows)
                    {
                        ddlBigClassid.Text = dr["BigClassid"].ToString().Trim();
                        this.txtTitle.Text = dr["Title"].ToString().Trim();
                        this.txtkeyWords.Text = dr["KeyWords"].ToString().Trim();

                        sPic = dr["Picture"].ToString().Trim();
                        if (sPic != "")
                        {
                            HPicture.Value = sPic;
                        }

                        this.txtWriter.Text = dr["Writer"].ToString().Trim();
                        this.txtWriterFrom.Text = dr["WriterFrom"].ToString().Trim();
                        this.Content.Text = dr["Content"].ToString().Trim();
                        this.rdoIsShow.Text = dr["IsShow"].ToString().Trim();
                        this.txtAddtime.Text = dr["Addtime"].ToString().Trim();
                    }
                }
                else
                {
                    this.txtAddtime.Text = DateTime.Now.AddDays(-1).ToString();
                }
            }
            catch
            {
            }
        }
    }
    /// <summary>
    /// 保存数据
    /// </summary>
    protected override void SavaData()
    {
        try
        {
            string sid = "";

            if (this.Request.Params["ID"] != null)
            {
                sid = this.Request.Params["ID"].Trim();
            }
            else
            {
                sid = "-1";
            }

            int BigClassid = int.Parse(this.ddlBigClassid.SelectedValue.ToString ().Trim ());//大类ID
            string sTitle = txtTitle.Text.ToString().Trim();
            string sKeyWords = txtkeyWords.Text.ToString().Trim();

            string stxtWriter = txtWriter.Text.ToString().Trim();
            string stxtWriterFrom = txtWriterFrom.Text.ToString().Trim();
            string sContent = Content.Text.ToString().Trim();
            string sIsShow = rdoIsShow.SelectedValue.ToString().Trim();
            DateTime DAddtime = Convert.ToDateTime(this.txtAddtime.Text.ToString ().Trim ());//发布日期

            string sPic = Pic.Value.ToString().Trim();

            //上传图片
            string sPhoto = "";
            if (sPic != "")
            {
                if (!Directory.Exists(Server.MapPath(".") + "\\UploadFile"))
                    Directory.CreateDirectory(Server.MapPath(".") + "\\UploadFile");

                int Len = Pic.PostedFile.ContentLength;
                if (Len < 204800)
                {
                    string PicType = sPic.Substring(sPic.LastIndexOf(".") + 1);
                    if (PicType.ToUpper() == "JPG" || PicType.ToUpper() == "GIF")
                    {
                        string FileName = sPic.Substring(sPic.LastIndexOf("\\") + 1);
                        string FileTime = string.Format("{0:yyyyMMddHHmmss}", System.DateTime.Now);
                        sPhoto = FileTime + FileName;
                        //Request.Files[0].SaveAs(Server.MapPath("UploadFile") + "\\" + sPhoto);
                        Pic.PostedFile.SaveAs(Server.MapPath(".") + "\\UploadFile\\" + sPhoto);
                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择JPG、GIF格式的图片上传！')</script>");
                        return;
                    }
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('上传的图片要小于200K！')</script>");
                    return;
                }
            }

            if (sPhoto.Trim() == "")  //本次没有更新图片;假如原来有图片,则还用原来的图片
            {
                sPhoto = HPicture.Value;
            }

            string sEditStatus = this.Request.QueryString["EditStatus"].ToString().Trim();
            if (sEditStatus == "1")
            {
                int iback = MyNews.InsertNewsInfo(BigClassid, 0, sTitle, sContent, sKeyWords, stxtWriter, stxtWriterFrom, 0, 0, sPhoto, "", sIsShow, DAddtime);
                if (iback > 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功!');location.href='NewsManage.aspx';</script>");
                    return;
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存失败!');</script>");
                    return;
                }
            }
            else
            {
                int iback = 0;
                try
                {
                    iback = MyNews.UpdateNewsInfo(BigClassid, 0, sTitle, sContent, sKeyWords, stxtWriter, stxtWriterFrom, 0, 0, sPhoto, "", sIsShow, DAddtime, sid);
                }
                catch
                {
                }

                if (iback > 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功!');location.href='NewsManage.aspx';</script>");
                    return;
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改失败!');</script>");
                    return;
                }
            }
        }
        catch (Exception ex)
        {
            Message.AlertMsg(this, "保存失败");
        }

    }
    #endregion
}
